﻿using DotNetHackathon.GBA.ApiCommon;
using Microsoft.AspNetCore.Mvc;
using Vaccine.API.Models;
using Vaccine.API.Repositories;

namespace Vaccine.API.Controllers
{
    /// <summary>
    /// 疫苗管理 api
    /// </summary>
    [ApiController]
    [Route("api/v1/vaccine")]
    public class VaccineController : ControllerBase
    {
        private readonly IVaccineInfoRepository _vaccineInfoRepository;

        public VaccineController(IVaccineInfoRepository vaccineInfoRepository)
        {
            _vaccineInfoRepository = vaccineInfoRepository;
        }

        /// <summary>
        /// 获取疫苗信息
        /// </summary>
        /// <param name="id">疫苗信息Id</param>
        /// <returns></returns>
        [Route("info/{id}")]
        [HttpGet]
        public async Task<ActionResult> GetVaccineInfoAsync(Guid id)
        {
            var vaccineInfo = await _vaccineInfoRepository.GetByIdAsync(id);

            if (vaccineInfo == null)
            {
                return Ok(vaccineInfo);
            }

            return Ok(MapFrom(vaccineInfo));
        }

        /// <summary>
        /// 删除疫苗信息
        /// </summary>
        /// <param name="id">疫苗信息Id</param>
        /// <returns></returns>
        [Route("info/{id}")]
        [HttpDelete]
        public async Task<ActionResult> DeleteVaccineInfoAsync(Guid id)
        {
            await _vaccineInfoRepository.DeleteAsync(id);
            return Ok();
        }

        /// <summary>
        /// 获取疫苗信息
        /// </summary>
        /// <param name="id">疫苗信息Id</param>
        /// <returns></returns>
        [Route("info/all")]
        [HttpGet]
        public async Task<ActionResult> GetAllVaccineInfoAsync()
        {
            var vaccineInfos = await _vaccineInfoRepository.GetAll();

            return Ok(vaccineInfos);
        }

        /// <summary>
        /// 新建疫苗信息
        /// </summary>
        /// <param name="vaccineInfoModel">疫苗信息</param>
        /// <returns></returns>
        [HttpPost("info")]
        public async Task<ActionResult> CreateVaccineInfoAsync([FromBody] CreateVaccineInfoInput vaccineInfoModel)
        {
            var vaccineInfo = new VaccineInfo()
            {
                Id = Guid.NewGuid(),
                CreatedTime = DateTime.Now,
                Description = vaccineInfoModel.Description,
                Name = vaccineInfoModel.Name,
                Quantity = vaccineInfoModel.Quantity
            };

            vaccineInfo = await _vaccineInfoRepository.AddAsync(vaccineInfo);

            return Ok(MapFrom(vaccineInfo));
        }

        /// <summary>
        /// 新建疫苗信息
        /// </summary>
        /// <param name="vaccineInfoModel">疫苗信息</param>
        /// <returns></returns>
        [Route("info")]
        [HttpPut]
        public async Task<ActionResult> UpdateVaccineInfoAsync([FromBody] UpdateVaccineInfoInput vaccineInfoModel)
        {
            var vaccineInfo = await _vaccineInfoRepository.GetByIdAsync(vaccineInfoModel.Id);

            if (vaccineInfo == null)
            {
                throw new BusinessException("notfound", "疫苗信息不存在");
            }

            vaccineInfo.LastModifiedTime = DateTime.Now;

            if (!string.IsNullOrEmpty(vaccineInfoModel.Name))
            {
                vaccineInfo.Name = vaccineInfoModel.Name;
            }

            if (!string.IsNullOrEmpty(vaccineInfoModel.Description))
            {
                vaccineInfo.Description = vaccineInfoModel.Description;
            }

            if (vaccineInfoModel.Quantity > 0)
            {
                vaccineInfo.Quantity = vaccineInfoModel.Quantity;
            }

            await _vaccineInfoRepository.UpdateAsync(vaccineInfo);

            return Ok();
        }

        private static VaccineInfoDto MapFrom(VaccineInfo vaccineInfo)
        {
            return new VaccineInfoDto()
            {
                Description = vaccineInfo.Description,
                Id = vaccineInfo.Id,
                Name = vaccineInfo.Name,
                Quantity = vaccineInfo.Quantity
            };
        }
    }
}
